﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ByteSite.Common
{
    public static class HttpManager
    {
        public static string HttpSendAsync(HttpMethod method, string url, Dictionary<string, string> headers = null, Dictionary<string, string> parms = null)
        {
            StringContent content = new StringContent("");

            if (method == HttpMethod.Get)
            {
                if (parms != null)
                {
                    if (url.IndexOf('?') > 0)
                    {
                        foreach (KeyValuePair<string, string> p in parms)
                        {
                            url += "&" + p.Key + "=" + p.Value;
                        }
                    }
                    else
                    {
                        int i = 0;
                        foreach (KeyValuePair<string, string> p in parms)
                        {
                            if (i == 0)
                                url += "?" + p.Key + "=" + p.Value;
                            else
                                url += "&" + p.Key + "=" + p.Value;
                            i++;
                        }
                    }
                }
                return Utils.HttpGet(url);
            }
            else
            {
                string strParams = string.Empty;
                if (parms != null)
                {
                    foreach (KeyValuePair<string, string> p in parms)
                    {
                        strParams += p.Key + "=" + p.Value + "&";
                    }
                    strParams = strParams.TrimEnd('&');
                }
                return Utils.HttpPost(url, strParams);
            }
        }
    }
}
